package com.athuahua.spzx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: huahua
 * @Date: 2024/8/17 0017
 * @Time: 6:01
 * @Description:
 */
@Configuration
@EnableAsync //启动异步方式运行
public class ThreadPoolConfig {
    @Bean("mailThreadPool")
    public Executor asyncTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置初始化的线程数
        executor.setCorePoolSize(10);
        //设置最大线程数
        executor.setMaxPoolSize(20);
        //设置缓冲队列容量
        executor.setQueueCapacity(500);
        //设置允许线程空闲时间，当超时了除了核心线程之外的空闲会被销毁
        executor.setKeepAliveSeconds(60);
        //设置线程名前缀
        executor.setThreadNamePrefix("async-task-executor");
        //缓冲队列满了之后的拒接策略，由调用线程处理（一般主线程）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
        executor.initialize();
        return executor;
    }



}
